/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.order;

import com.x3.purchasing.account.Account;
import com.x3.purchasing.account.AccountDAO;
import com.x3.purchasing.account.AccountDAOImpl;
import com.x3.purchasing.common.ClassApplicationModule;
import com.x3.purchasing.operator.Operator;
import com.x3.purchasing.operator.OperatorDAO;
import com.x3.purchasing.operator.OperatorDAOImpl;
import com.x3.purchasing.produk.Produk;
import com.x3.purchasing.produk.ProdukDAO;
import com.x3.purchasing.produk.ProdukDAOImpl;
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.zkoss.image.AImage;
import org.zkoss.image.Image;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Div;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Groupbox;
import org.zkoss.zul.Hbox;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Row;
import org.zkoss.zul.Rows;
import org.zkoss.zul.Space;
import org.zkoss.zul.Toolbarbutton;
import org.zkoss.zul.Vbox;

/**
 *
 * @author kris
 */
public class OrderWnd extends ClassApplicationModule {

    private OrderDAO orderDAO;
    private OrderDetailDAO orderDetailDAO;
    private ProdukDAO produkDAO;
    private OperatorDAO operatorDAO;
    private Groupbox groupDetail;
    private AccountDAO accountDAO;
    private Grid gridDetail;
    private Account account;
    private Session session;
    private Intbox txtJumlah;
    private DecimalFormat df = new DecimalFormat("#0,000.00");

    public void onCreate() throws InterruptedException, IOException, Exception {
        orderDAO = new OrderDAOImpl();
        orderDetailDAO = new OrderDetailDAOImpl();
        produkDAO = new ProdukDAOImpl();
        operatorDAO = new OperatorDAOImpl();
        accountDAO = new AccountDAOImpl();
        groupDetail = (Groupbox) this.getFellow("groupDetail");
        gridDetail = (Grid) this.getFellow("gridDetail");
        session = Sessions.getCurrent();
        account = (Account) session.getAttribute("ACC_LOGIN");
        this.loadDetail();
    }

    private void loadDetail() throws IOException, Exception {
        final Produk produk = produkDAO.get((Integer) session.getAttribute("produk_id"));
        Rows rows = new Rows();
        Row row = new Row();
        Hbox hbox = new Hbox();
        if (produk.getFileGambar() != null) {
            if (produk.getFileGambar().available() > 0) {
                org.zkoss.zul.Image img = new org.zkoss.zul.Image();
                Image image = new AImage("t", produk.getFileGambar());
                img.setContent(image);
                img.setVspace("10");
                img.setHspace("10");
                img.setHeight("300px");
                img.setWidth("300px");
                hbox.appendChild(img);
            }
        } else {
            Label lblError = new Label("Gambar tidak tersedia");
            lblError.setStyle("color:red;");
            hbox.appendChild(lblError);
        }

        Vbox vbox = new Vbox();
        Space space = new Space();
        space.setWidth("10");
        vbox.appendChild(space);
        vbox.appendChild(new Label("Kode : " + produk.getKode()));
        vbox.appendChild(new Label("Nama : " + produk.getNama()));
        vbox.appendChild(new Label("Keterangan : " + produk.getKeterangan()));
        vbox.appendChild(new Label("Harga : " + df.format(produk.getHargaSatuan())));
        vbox.appendChild(new Label("Vendor : " + produk.getVendor().getNama()));
        txtJumlah = new Intbox();
        txtJumlah.setValue(1);
        vbox.appendChild(new Label("Jumlah : "));
        vbox.appendChild(txtJumlah);

        Toolbarbutton btnAdd = new Toolbarbutton();
        btnAdd.setTooltiptext("Add to cart");
        btnAdd.setImage("/img/add_cart.gif");
        btnAdd.addEventListener("onClick", new EventListener() {

            public void onEvent(Event event) throws Exception {
                List<OrderDetail> orderDetails = null;
                Operator operator = null;
                try {
                    operator = operatorDAO.getByAccount(account.getId());
                    Order order = new Order();
                    order.setTanggal(new Date());
                    order.setStatus(0);
                    order.setOperator(operator);

                    OrderDetail orderDetail = new OrderDetail();
                    orderDetail.setProduk(produk);
                    orderDetail.setOrder(order);
                    orderDetail.setHargaSatuan(produk.getHargaSatuan());
                    orderDetail.setJumlah(txtJumlah.getValue());
                    if (session.getAttribute("ORDER") == null) {
                        orderDetails = new ArrayList<OrderDetail>();
                    } else {
                        orderDetails = (List<OrderDetail>) session.getAttribute("ORDER");
                    }
                    orderDetails.add(orderDetail);
                    session.setAttribute("ORDER", orderDetails);
                    redirect("/zul/Cart.zul");
                } catch (Exception e) {
                    redirect("/zul/login.zul");
                }
            }
        });
        vbox.appendChild(btnAdd);
        hbox.appendChild(vbox);
        row.appendChild(hbox);
        rows.appendChild(row);
        gridDetail.appendChild(rows);
    }

    public void redirect(String url) throws Exception {
        Div contentDiv = (Div) this.getParent();
        contentDiv.getChildren().clear();
        Executions.createComponents(url, contentDiv, null);
    }
}
